    //
//  PromotionViewController.m
//  iShop
//
//  Created by svp on 1/11/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "PromotionViewController.h"
#import "ResourceLoader.h"
#import "TextViewCell.h";
#import "TitleTextfieldCell.h"


@implementation PromotionViewController

@synthesize tableView = _tableView;
@synthesize promotion = _promotion;
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(IBAction) cancelPress:(id) sender;
{
	[self.parentViewController dismissModalViewControllerAnimated:YES];
}

- (void)dealloc {
    [super dealloc];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 
{
	return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	return section == 0? @"Infomation": @"Description";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return section == 0? 3 : 1;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
	return indexPath.section == 0 ? 44: 270;
}	

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (indexPath.section == 1) {
		TextViewCell *cell = (TextViewCell*)[tableView dequeueReusableCellWithIdentifier:@"TextViewCell"];
		if (!cell) {
			NSLog(@"Create cell");
			ResourceLoader *rc = [[[ResourceLoader alloc] initWithNibNamed:@"TextViewCell"] autorelease];
			cell = (TextViewCell*)rc.resource;		
			cell.textView.text = @"Happy New Year Promotion";
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
			cell.textView.scrollEnabled = YES;
		}
		return cell;
	}
	TitleTextfieldCell* cell = (TitleTextfieldCell*)[tableView dequeueReusableCellWithIdentifier:@"TitleTextfieldCell"];
	if (!cell) {
		ResourceLoader *rc = [[[ResourceLoader alloc] initWithNibNamed:@"TitleTextfieldCell"] autorelease];
		cell = (TitleTextfieldCell*)rc.resource;
		cell.textField.enabled = NO;
		cell.textField.textAlignment = UITextAlignmentRight;
	}
	NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
	[formatter setDateFormat:@"dd/MM/YYY"];
	switch (indexPath.row) {
		case 0:
			cell.titleLabel.text = @"Promo Name";
			cell.textField.text = _promotion.PromotionName;
			break;
		case 1:
			cell.titleLabel.text = @"Start Date";
			cell.textField.text = [formatter stringFromDate:_promotion.PromotionStart];
			break;
		case 2:
			cell.titleLabel.text = @"End Date";
			cell.textField.text = [formatter stringFromDate:_promotion.PromotionEnd];
			break;
		default:
			break;
	}
	return cell;
}

-(IBAction) savePress:(id) sender
{
}


@end
